package com.sunzhe.cms.config;

import com.ibeetl.admin.core.conf.JasonConfig;
import com.sunzhe.cms.service.CmsPlatformService;
import com.sunzhe.cms.tag.ArticleListTag;
import com.sunzhe.cms.tag.CatalogListTag;
import com.sunzhe.cms.tag.TestTag;
import com.sunzhe.cms.util.beetl.CatalogQueryFunction;
import org.beetl.core.GroupTemplate;
import org.beetl.core.tag.Tag;
import org.beetl.core.tag.TagFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

import javax.annotation.PostConstruct;

/**
 * 可以在这里注册标签，方法等内容
 * 
 * @author Administrator
 *
 */
@Configuration
@AutoConfigureAfter(JasonConfig.class)
//@EnableRedisHttpSession         //开启spring session支持
public class CmsConfig {
	@Autowired
	GroupTemplate groupTemplate;
	@Autowired
	ApplicationContext applicationContext;
	@Autowired
	CatalogQueryFunction catalogQueryFunction;
	@Autowired
	CmsPlatformService platFormService;

	@PostConstruct
	public String init() {
		groupTemplate.registerFunctionPackage("cms", platFormService);

		//设置Tag
		groupTemplate.registerTagFactory("cmsCatalogList", new TagFactory() {
			@Override
			public Tag createTag() {
				return applicationContext.getBean(CatalogListTag.class);
			}
		});
		
		groupTemplate.registerTagFactory("cmsArticleList", new TagFactory() {
			@Override
			public Tag createTag() {
				return applicationContext.getBean(ArticleListTag.class);
			}
		});
		groupTemplate.registerTagFactory("testTag", new TagFactory() {
			@Override
			public Tag createTag() {
				return applicationContext.getBean(TestTag.class);
			}
		});
		groupTemplate.registerFunctionPackage("catalog", catalogQueryFunction);
		return null;
	}

	@Value("${custom.cookie.domain}")
	private String cookieDomainName;
	@Value("${custom.cookie.name}")
	private String cookieName;
	@Value("${custom.cookie.path}")
	private String cookiePath;

	@Bean
	public CookieSerializer cookieSerializer() {
		DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
		//cookie名字
		defaultCookieSerializer.setCookieName(cookieName);
		//域
		defaultCookieSerializer.setDomainName(cookieDomainName);
		//存储路径
		defaultCookieSerializer.setCookiePath(cookiePath);
		return defaultCookieSerializer;
	}
}
